package com.jxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxx.mapper.ShopcartMapper;
import com.jxx.pojo.Item;
import com.jxx.pojo.Shopcart;
import com.jxx.service.ShopcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ShopcartServiceImpl implements ShopcartService {
    @Autowired
    private ShopcartMapper shopcartMapper;


    @Override
    public List<Item> findShoppingItem(Integer userId) {
      List<Item> findItem=shopcartMapper.findUserItem(userId);
      return findItem;
    }

    @Override
    public void deleteItemId(Long [] itemid) {
        List<Long> longList = Arrays.asList(itemid);
        shopcartMapper.deleteBatchIds(longList);
        System.out.println("删除成功");
    }

    @Override
    public void deleteItem(Integer itemid) {
        shopcartMapper.deleteById(itemid);
        System.out.println("删除成功");
    }

    /**
     * 商品购物车添加,更新
     * @param shopcart
     */
    @Override
    public void addShopCart(Shopcart shopcart) {
        Integer userId =shopcart.getUserId();
        Integer itemId=shopcart.getItemId();
        QueryWrapper<Shopcart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("item_id", itemId);
        Shopcart shopcartDB=shopcartMapper.selectOne(queryWrapper);
        if (shopcartDB==null){
            shopcartMapper.insert(shopcart);
        }else {
            int shopcartNum=shopcartDB.getShopcartNum()+shopcart.getShopcartNum();
            Shopcart shopcartTemp=new Shopcart();
            shopcartTemp.setShopcartNum(shopcartNum).setShopcartId(shopcartDB.getShopcartId());
            System.out.println(shopcartTemp);
            shopcartMapper.updateById(shopcartTemp);
        }

    }
//    @Override
//    public void updateCartNum(Shopcart shopcart) {
//        Shopcart cartTemp = new Shopcart();
//        cartTemp.setShopcartNum(shopcart.getShopcartNum());
//        QueryWrapper<Shopcart> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id", shopcart.getUserId())
//                .eq("item_id", shopcart.getItemId());
//        shopcartMapper.update(cartTemp,queryWrapper);
//        //参数1: 封装set条件   参数2:封装where条件
//    }


}
